<?php
class Model_Util {

    public static function getBuildingType()
    {
        return array(
            1 => 'lowrise',
            'mediumrise',
            'highrise'
        );
    }

    public static function getFloorZone()
    {
        return array(
            1 => 'Ground floor',
            'low',
            'mid',
            'high',
        );
    }

    public static function getResidentialType()
    {
        return array(
            '' => 'All',
            'All Property' => 'All Property',
            'Apartments For Rent' => 'Apartments For Rent',
            'Apartments For Sale' => 'Apartments For Sale',
            'Break Lease' => 'Break Lease',
            'Flatshare/Houseshare' => 'Flatshare/Houseshare',
            'Houses For Rent' => 'Houses For Rent',
            'Houses For Sale' => 'Houses For Sale',
            'Land Investment' => 'Land Investment',
        );
    }

    public static function getCommercialType()
    {
        return array(
            '' => 'All',
            'Offices For Rent' => 'Offices For Rent',
            'Offices For Sale' => 'Offices For Sale',
            'Office Share' => 'Office Share',
            'Other Investments' => 'Other Investments',
            'Parking For Rent' => 'Parking For Rent',
            'Parking For Sale' => 'Parking For Sale',
            'Property Management' => 'Property Management',
            'Property Wanted' => 'Property Wanted',
            'Serviced Apartments' => 'Serviced Apartments',
            'Shops For Rent' => 'Shops For Rent',
            'Shops For Sale' => 'Shops For Sale',
            'Short-Term' => 'Short-Term',
            'Storage For Rent' => 'Storage For Rent',
            'Storage For Sale' => 'Storage For Sale',
            'Sublets' => 'Sublets',
            'Swaps & Exchanges' => 'Swaps & Exchanges',
            'Warehouses For Rent' => 'Warehouses For Rent',
            'Warehouses For Sale' => 'Warehouses For Sale',
            'Building For Rent' => 'Building For Rent',
            'Building For Sale' => 'Building For Sale'
        );
    }
    
    public static function parseUrl($url)
    {
    	if(strpos($url, 'https://') !== 0){
    		$url = 'http://' . str_replace('http://', '', $url);
    	}
    	
    	$url = iconv('', 'utf-8', $url);
    	$urlParsed = parse_url(strtolower($url));
    	if(isset($urlParsed['query'])){
    		parse_str($urlParsed['query'], $urlParsed['query']);
    	}
    	
    	return $urlParsed;
    }
    
    public static function againstBannedWords($contents)
    {	
    	$findWords = array();
    	if(is_object(Model_Config::one()))
    	{
    		$findWords = Model_Config::one()->keyword;
    	}
    	if(count($findWords) >0 )
    	{
	    	$replaceWords = array();
	    	$replaceWords =  array_pad($replaceWords, count($findWords), '');
	    	
	    	$contents_end = str_ireplace($findWords, $replaceWords, $contents);
	    	if($contents === $contents_end)
	    	{
	    		$contents = true;
	    	}
	    	else{
	    		$contents = false;//"Your comment will be reviewed by our moderators. Thank you.";
	    	}
	    	return $contents;
    	}
    	else{
    		return true;
    	}
    }
    
    
    /**
     * @param array/string $htmlTags html tag, example: array('img' or 'a' .eg)
     * @param string $content content
     * @param string/bool/array $isSelfEnd <br />
     * &nbsp;&nbsp;&nbsp;&nbsp;
     * default - 'auto', it takes the these htmlTag as self-closing tag: 'img', 'br', 'script', 'hr', 'input', 'link', 'meta', 'area', 'base', 'basefont', 'frame', 'col'<br />
     * @return array $ret 
     * <br />
     * )
     */
    public static function htmlRegCollection($htmlTags = '', $content, $isSelfEnds = 'auto', $trimResult = true, $enCoding = 'utf-8')
    {
	    $matchesArr = array();
    	$matchKey = array(
    		0 => 'ELEMENT_ENTIRE',
    		1 => 'ELEMENT_START_TAG',
    		2 => 'ELEMENT_INNER',
    		3 => 'ELEMENT_END_TAG'
    	);
    	if(is_string($htmlTags)){
    		$htmlTags = array($htmlTags);
    	}
    	if(!is_array($isSelfEnds)){
    		$isSelfEnds = array($isSelfEnds);
    	}
    	$isSelfEnds = array_pad($isSelfEnds, count($htmlTags), 'auto');	
    	foreach($htmlTags as $htmlTagCount => $htmlTag){
    		$isSelfEnd = $isSelfEnds[$htmlTagCount];
	    	if($isSelfEnd == 'auto'){
	    		if(in_array(strtolower($htmlTag), array(
	    			'img', 'br', 'script', 'hr', 'input', 'link', 'meta', 'area', 'base', 'basefont', 'frame', 'col'
	    		))){
	    			$isSelfEnd = true;
	    		}else{
	    			$isSelfEnd = false;
	    		}
	    	}    
    		if($isSelfEnd){
    			$reg = "/<$htmlTag"."[\s\S]*?\/?>/i";
	    	}else{
	    		$reg = "/(<$htmlTag"."[\s\S]*?>)([\s\S]*?)(<\/$htmlTag>)/i";
	    	}	
	    	
    	    if(preg_match_all($reg, $content, $matches, PREG_SET_ORDER)){
	    		foreach($matches as $count => $match){
	    			foreach($match as $k => $v){
	    				$encodingFrom = mb_detect_encoding($v, array('ASCII', 'CP936', 'GB2312', 'GBK', 'BIG5'));
	    				if($encodingFrom){
	    					$v = mb_convert_encoding($v, $enCoding, $encodingFrom);
	    				}
	    				$matchesArr[$htmlTag][$count][$matchKey[$k]] = ($trimResult)? preg_replace('/\s{2,}/', ' ', $v) : $v;
	    			}
	    		}
	    	}
    	}
    	
		return $matchesArr;
    }
    
    
    
    
    
    
    
    
}